﻿<%@ Page Title="" Language="C#" MasterPageFile="~/EasyUI.Master" AutoEventWireup="true" CodeBehind="show.aspx.cs" Inherits="jnlamp.Web.lampinfo.show" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
     <link href="../CSS/IconExtension.css" rel="stylesheet" />
    <script src="../Script/export.js"></script>
    <script src="../Script/username.js"></script>
    <script src="../Script/datetime.js"></script> 
    <script src="../Script/highcharts.js"></script>  
    <script src="../Script/exporting.js"></script>
    <script src="../Script/jnbase.js"></script>
    <%--<script src='../ckeditor/ckeditor.js"'></script>
    <script src='../ckeditor/adapters/jquery.js'></script>--%>
    <script type="text/javascript">
        var uid = 0;
        var gwid = "";
        var lampid = "";
        var mchs = "电池板电压,蓄电池电压,电池板电流,杀虫灯电流";//,电池板状态
        var chs = mchs.split(',');;
        var mpet = "";
        var mpetv = "";
        var griddata = "";
        var cadd = "";
        var dtOpstart = "";
        var dtOpend = "";
        var filterObj = "";
        var add = "";
        var uname = "";
        var qxchs;
        var yjfx = "";
        var level = 0;
        Array.prototype.deleteElementByValue = function (varElement) {
            var numDeleteIndex = -1;
            for (var i = 0; i < this.length; i++) {
                // 严格比较，即类型与数值必须同时相等。
                if (this[i] === varElement) {
                    this.splice(i, 1);
                    numDeleteIndex = i;
                    break;
                }
            }
            return numDeleteIndex;
        }
        function getfids(rows) {
            var ids = "";
            for (var i = 0; i < rows.length; i++)
                ids = ids + rows[i].fp_id + ",";
            if (ids != "")
                ids = ids.substr(0, ids.length - 1);
            return ids;

        }
        function getids(rows) {
            var ids = "";
            for (var i = 0; i < rows.length; i++)
                ids = ids + rows[i].uid + ",";
            if (ids != "")
                ids = ids.substr(0, ids.length - 1);
            return ids;

        }        
        
        $(function () {
            $.ajax({
                type: "post",
                dataType: "json",
                url: "../admin/getinfo.ashx?action=level",
                success: function aa(data) {
                    var level = data[0].level;
                    if (level != 5 && level != 1) {
                        $("#a_del").hide();
                    }
                }
            });
            year = new Date().getFullYear();
            $('#jd').combobox({
                onSelect: function (newValue, oldValue) {
                    if (newValue.text == '一季度') {
                        $("#StartTime").datebox("setValue", year + "-1-1");
                        $("#EndTime").datebox("setValue", year + "-3-31");
                    }
                    else if (newValue.text == '二季度') {
                        $("#StartTime").datebox("setValue", year + "-4-1");
                        $("#EndTime").datebox("setValue", year + "-6-30");
                    }
                    else if (newValue.text == '三季度') {
                        $("#StartTime").datebox("setValue", year + "-7-1");
                        $("#EndTime").datebox("setValue", year + "-9-30");
                    }
                    else if (newValue.text == '四季度') {
                        $("#StartTime").datebox("setValue", year + "-10-1");
                        $("#EndTime").datebox("setValue", year + "-12-31");
                    }
                    else if (newValue.text == '当天') {
                        var curr_time = new Date();
                        var tc_time = new Date(curr_time.getTime() + 24 * 60 * 60 * 1000);
                        var strDate = curr_time.getFullYear() + "-";
                        strDate += curr_time.getMonth() + 1 + "-";
                        strDate += curr_time.getDate();
                        var strDateend = tc_time.getFullYear() + "-";
                        strDateend += tc_time.getMonth() + 1 + "-";
                        strDateend += tc_time.getDate();
                        $("#StartTime").datebox("setValue", strDate);
                        $("#EndTime").datebox("setValue", strDateend);
                    }
                }
            });
            var curr_time = new Date();
            var tc_time = new Date(curr_time.getTime() + 24 * 60 * 60 * 1000);
            var strDate = curr_time.getFullYear() + "-";
            strDate += curr_time.getMonth() + 1 + "-";
            strDate += curr_time.getDate();
            var strDateend = tc_time.getFullYear() + "-";
            strDateend += tc_time.getMonth() + 1 + "-";
            strDateend += tc_time.getDate();
            $("#StartTime").datebox("setValue", strDate);
            $("#EndTime").datebox("setValue", strDateend);
            gwid = getUrlParam("gwid");
            lampid = getUrlParam("lampid");            
            InitGird();

        });
        
        function InitGird() {
            $('#tt').datagrid({
                title: '设备运行状态', //表格标题
                url: 'GetScd.ashx', //请求数据的页面
                sortName: 'inputTime', //排序字段
                idField: 'fp_id', //标识字段,主键
                iconCls: '', //标题左边的图标
                width: '100%', //宽度
                nowrap: false, //是否换行，True 就会把数据显示在一行里
                striped: true, //True 奇偶行使用不同背景色
                collapsible: false, //可折叠
                sortOrder: 'desc', //排序类型
                remoteSort: true, //定义是否从服务器给数据排序
                fitColumns: true,
                singleSelect: false,
                method: 'post',
                columns: [[//
                   //电池板电压	蓄电池电压	电池板电流	杀虫灯电流	电池板状态	杀虫灯开闭状态	杀虫灯开闭时长	上传时间
                    { title: '电池板电压', field: 'dcb_v', width: 150},
                    { title: '蓄电池电压', field: 'xdc_v', width: 150 },
                    {
                        title: '电池板电流', field: 'dcb_i', width: 150, formatter: function (value, row, index) {
                            if (value == "True")
                                return '有电流';
                            else
                                return '无电流';
                        }
                    },
                    {
                        title: '杀虫灯电流', field: 'scd_i', width: 150, formatter: function (value, row, index) {
                            if (value == "True")
                                return '有电流';
                            else
                                return '无电流';
                        }
                    },
                    {
                        title: '电池板状态', field: 'dcb_s', width: 150, formatter: function (value, row, index) {
                            if (value == "1")
                                return '正常';
                            else
                                return '异常或被盗';
                        }
                    },
                    {
                        title: '杀虫灯开闭状态', field: 'scd_s', width: 150, formatter: function (value, row, index) {
                            if (value == "0")
                                return '<img src="/images/bullet_red.png" height="16" width="16"/>';
                            else
                                return '<img src="/images/bullet_green.png" height="16" width="16" />';
                        }
                    },
                    { title: '杀虫灯开闭时长', field: 'scd_t', width: 150 },                    
                    {
                        title: '是否上传数据', field: 'datetime', width: 80, formatter: function (value, row, index) {
                            if (row.inputTime == "" || row.inputTime == null)
                                return '<img src="/images/bullet_red.png" height="16" width="16"/>';
                            else
                                return '<img src="/images/bullet_green.png" height="16" width="16" />';
                        }
                    },
                    {
                        title: '上传时间', field: 'inputTime', width: 200, formatter: function (value, row, index) {
                            if (value != "") {

                                if (value.indexOf("-") != -1)
                                    value = value.replace(/-/g, "/")
                                return new Date(value).pattern("yyyy年M月d日 HH:mm:ss");
                            }
                        }
                    }
                ]
                ],                
                toolbar: "#tab_toolbar",
                queryParams: { "action": "query","gwid":gwid, "lampid": lampid, "StartTime": $("#StartTime").datebox("getValue"), "EndTime": $("#EndTime").datebox("getValue")},
                pagination: true, //是否开启分页
                pageNumber: 1, //默认索引页
                pageSize: 20, //默认一页数据条数
                rownumbers: true,
                onClickRow: function (rowIndex, rowData) {
                    var dl = "";
                    if (rowData.dcb_i == "True")
                        dl= '有电流';
                    else
                        dl = '无电流';
                    var zt = "";
                    if (rowData.dcb_s == "1")
                        zt= '正常';
                    else
                        zt = '异常或被盗';
                    
                    var scd = "";
                    if (rowData.scd_s == "0")
                        scd = "开";
                    else
                        scd="关";
                    yjfx = "网关ID" + gwid + "灯ID" + lampid + ",在" + rowData.inputTime + '电池板电压' + rowData.dcb_v + '蓄电池电压' + rowData.xdc_v + '电池板'+dl + '电池板状态' + zt + '杀虫灯' + scd;
                    $('#yjfx').text(yjfx);
                },
                onLoadSuccess: function (data) {
                    WChartJS.init(chs);
                    var t1 = "", t2 = "", t3 = "", t4 = "";
                    var dcb = "", scb = "";
                    var md = "";                                    
                    $.each(data.rows, function (k, v) {
                        md = v["inputTime"];
                        md = md.replace(/-/g, "/");
                        md = Date.parse(md);
                        if (v["dcb_i"] == "True") dcb = 5;
                        else dcb = 0;
                        if (v["scd_i"] == "True") scb = 5;
                        else scb = 0;
                        t1 = "[" + md + "," + v["dcb_v"] + "]," + t1;
                        t2 = "[" + md + "," + v["xdc_v"] + "]," + t2;
                        t3 = "[" + md + "," + dcb + "]," + t3;
                        t4 = "[" + md + "," + scb + "]," + t4;

                    });                    
                    t1 = "[" + t1 + "]";
                    t2 = "[" + t2 + "]";
                    t3 = "[" + t3 + "]";
                    t4 = "[" + t4 + "]";
                    wchartInstance.series[0].setData(eval(t1));
                    wchartInstance.series[1].setData(eval(t2));
                    wchartInstance.series[2].setData(eval(t3));
                    wchartInstance.series[3].setData(eval(t4));
                    
                    //$('#yjfx').text(result);

                }
            });
            Highcharts.setOptions({
                lang: {
                    contextButtonTitle: "图表导出菜单",
                    decimalPoint: ".",
                    downloadJPEG: "下载JPEG图片",
                    downloadPDF: "下载PDF文件",
                    downloadPNG: "下载PNG文件",
                    downloadSVG: "下载SVG文件",
                    drillUpText: "返回 {series.name}",
                    loading: "加载中",
                    months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
                    noData: "没有数据",
                    numericSymbols: ["千", "兆", "G", "T", "P", "E"],
                    printChart: "打印图表",
                    resetZoom: "恢复缩放",
                    resetZoomTitle: "恢复图表",
                    shortMonths: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
                    thousandsSep: ",",
                    weekdays: ["星期一", "星期二", "星期三", "星期三", "星期四", "星期五", "星期六", "星期天"]
                }
            });

        }
        function getQueryParams(queryParams) {
            var StartTime = $("#StartTime").datebox("getValue");
            var EndTime = $("#EndTime").datebox("getValue");           
            queryParams.StartTime = StartTime;
            queryParams.EndTime = EndTime;            
            return queryParams;

        }
        //增加查询参数，重新加载表格  
        function reloadgrid() {
            var queryParams = $('#tt').datagrid('options').queryParams;
            getQueryParams(queryParams);
            $('#tt').datagrid('options').queryParams = queryParams;
            $("#tt").datagrid('reload');

        }
        var wchartInstance;
        var WChartJS = {
            init: function (chs) {
                var s = "[";
                $.each(chs, function () {
                    s = s + "{name:'" + this + "',data:[]},";
                });
                s = s.substr(0, s.length - 1);
                s = s + "]";
                wchartInstance = new Highcharts.Chart({
                    chart: {
                        renderTo: 'chartArea',
                        type: 'line'
                    },
                    credits: {
                        enabled: false
                    },
                    exporting: {
                        enabled: true
                    },
                    title: {
                        text: '',
                        x: -20 //center
                    },
                    xAxis: {
                        type: 'datetime',
                        labels: {
                            step: 4,
                            formatter: function () {
                                return new Date(this.value).toLocaleDateString();
                            }
                        }
                    },
                    plotOptions: {
                        series: {
                            events: {
                                legendItemClick: function () {
                                    var visibility = this.visible ? 'visible' : 'hidden';
                                    if (visibility == 'hidden') {
                                        for (var i = 0; i < qxchs.length; i++) {
                                            wchartInstance.series[i].hide();
                                        }
                                    }
                                    return true;
                                }
                            }
                        }
                    },
                    yAxis: {
                        title: {
                            text: '数值'
                        },
                        plotLines: [{
                            value: 0,
                            width: 1,
                            color: '#808080'
                        }]
                    },
                    //tooltip: {
                    //    formatter: function () {                   

                    //},
                    legend: {
                        //layout: 'vertical',
                        align: 'center',
                        verticalAlign: 'top',
                        borderWidth: 0
                    },
                    series: eval(s)
                });
            }
        };
        function remove() {
            var row = $('#tt').datagrid('getSelections');
            if (row == null || row.length == 0) {
                $.messager.alert("提示", "请选择要删除的行！", "info");
            }
            if (row) {
                $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
                    if (r) {
                        var ids = getfids(row);
                        jQuery.getJSON('GetScd.ashx?action=del&ids=' + ids, null, function (data) {
                            if (data[0].r == "T") {
                                $.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                                reloadgrid();
                            }
                            else {
                                $.messager.alert("提示", "信息删除失败！", "info");
                            }
                        });
                    }
                });
            }
        }
        function showAlarm()
        {
            $('#dlg').dialog('open').dialog('setTitle', '预警信息发送');
            InitSendUser();
            $('#mmark').val(yjfx);
        }
        function InitSendUser() {
            $('#senduser').combotree({
                url: '../userinfo/GetLinker.ashx?action=queryt',
                multiple: true,
                width: 300,
                valueField: 'id',
                textField: 'text',
                //panelHeight: 'auto'
                panelHeight: 160

            });
            $('#mmark').ckeditor();
            for (instance in CKEDITOR.instances)
                CKEDITOR.instances[instance].setData("");

        }
        function CKupdate() {
            for (instance in CKEDITOR.instances)
                CKEDITOR.instances[instance].updateElement();
        }
        function sendAlarm() {
            CKupdate();
            var f = $('#myalarm');
            var uids = $('#senduser').combotree('getValues');
            var alevel = $('#alarmlevel').val();
            $.messager.progress();

            f.form('submit', {
                url: "../info/GetInfo.ashx?action=adds&uid=" + uids + "&alarmclass=",
                onSubmit: function () {
                    var isval = $(this).form('validate');
                    if (isval == false) $.messager.progress('close');
                    return isval;
                },
                success: function (result) {
                    $.messager.progress('close');
                    if (result.indexOf("T") == 0) {
                        $('#dlg').dialog('close');
                        $.messager.alert("提示", "恭喜您，信息发送成功", "info");                        
                    }
                    else if ((result.indexOf("F") == 0)) {
                        $.messager.alert("提示", "发送失败，请重新操作！", "info");
                        return;
                    }
                    else if ((result.indexOf("L") == 0)) {
                        $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                        top.location = '/index.aspx';
                        return;
                    }
                }
            });
        }
        </script>
    <style>
        .fitem {
            float: left;
        }

            .fitem label {
                width: 30px;
                line-height: 24px;
            }
             .dl1 {
            color:green;
            font-weight:bold;
        }
        .dl2 {
            color:blue;
            font-weight:bold;
        }
        .dl3 {
            color:yellow;
            font-weight:bold;
        }
        .dl4 {
            color:red;
            font-weight:bold;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="easyui-layout" data-options="fit:true">  
            
    <div data-options="region:'north'" style="padding:5px;background:#eee;height:300px;">

    <table id="tt">
    </table>
    </div>
        <div id="chartArea" data-options="region:'center',split:true"></div>    
    <div  id="yjfx" data-options="region:'south',title: '灯分析',split:true,tools: [{ iconCls: 'icon-phone', handler: function () { showAlarm(); } }]" style="height:60px;">
        灯分析：
    </div>   
   
    
</div>  


    <div id="tab_toolbar" style="padding: 4px;">
        <table cellpadding="0" cellspacing="0" style="width: 100%">
            <tr>
                <td style="padding-left: 2px">

                    <div style="padding: 2px;">
                        时间从:  
           <input id="StartTime" class="easyui-datebox" style="width: 100px" />
                            到:  
           <input id="EndTime" class="easyui-datebox" style="width: 100px" />
                         <select id="jd" class="easyui-combobox" name="jd" style="width:80px;">
                    <option value="季度">季度</option>
                                <option value="一季度">一季度</option>
                                <option value="二季度">二季度</option>
                                <option value="三季度">三季度</option>
                                <option value="四季度">四季度</option>
                    <option value="当天">当天</option>
</select>                           
                        
                            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="reloadgrid()">查找</a>
                            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="Export('excel', $('#tt'));">导出</a>
                             
                              <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="CRUD.init();">分析</a> 
                        <a href="#" onclick="remove();return false;" id="a_del" class="easyui-linkbutton" iconcls="icon-cancel">删除</a>          
                    </div>
                </td>
            </tr>
        </table>
    </div>
   
      <div id="dlg" class="easyui-dialog" style="width: 600px; height:450px; padding: 10px 20px"
                        data-options="closed:true,buttons:'#dlg-buttons',shadow:false">
                   <form id="myalarm" method="post">
                    <div class="fite">
                        <label style="width:auto;">发送给：</label>
                        <input name="senduser" id="senduser" />
                    </div>                   
     <div class="fitem" style="clear:none;width:380px;float:left;">
        <label style="width:auto;">标题：</label>
        <input name="alarmtitle" id="alarmtitle" style="width:280px;" />
    </div>
     <div class="fitem" style="width:200px;float:left; clear:none;">
        <label style="width:auto;">预警级别：</label>
        <select id="alarmlevel" name="alarmlevel">
            <option value="普通" selected="selected">普通</option>
            <option value="一级">一级</option>
            <option value="二级">二级</option>
            <option value="三级">三级</option>
            <option value="四级">四级</option>
        </select>
    </div>
                    <div style="clear: both;"></div>
                    <div>
                        <textarea id="mmark" name="mmark" ></textarea>
                    </div>    
                </form>
                    </div>
                    <div id="dlg-buttons">
                        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="sendAlarm()">预警信息发布</a>
                        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close')">关闭</a>
                    </div>
</asp:Content>

